package com.house.system.service.impl;

import java.util.List;

import com.house.common.annotation.DataScope;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.core.email.MailSender;
import com.house.common.utils.DateUtils;
import com.house.system.domain.Medium;
import com.house.system.domain.RentalType;
import com.house.system.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.house.system.domain.Contract;
import com.house.system.service.IContractService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同信息Service业务层处理
 *
 * @author tc
 * @date 2022-02-04
 */
@Service
@Slf4j
public class ContractServiceImpl implements IContractService {
    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private RentalTypeMapper rentalTypeMapper;

    @Autowired
    private MailSender mailSender;

    @Autowired
    private MediumMapper mediumMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询合同信息
     *
     * @param contractId 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contract selectContractByContractId(Long contractId) {
        return contractMapper.selectContractByContractId(contractId);
    }

    /**
     * 查询合同信息列表
     *
     * @param contract 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同信息
     *
     * @param contract 合同信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertContract(Contract contract) {
        //插入合同为用户申请租房,不添加发布时间
        return contractMapper.insertContract(contract);
    }

    /**
     * 修改合同信息
     *
     * @param contract 合同信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateContract(Contract contract) {
        //合同生效
        contract.setCreateTime(DateUtils.getNowDate());
        contract.setStatus("0");
        //根据houseId获取房屋信息和租售类型,判断是否需要更改租售状态
        House house = houseMapper.selectHouseByHouseId(contract.getHouseId());
        RentalType rentalType = rentalTypeMapper.selectRentalTypeByHouseId(contract.getHouseId());
        //查询招租人数
        int needed = rentalType.getNeeded();
        //查询已租人数
        int rented = rentalType.getRented();
        rented += 1;
        rentalType.setRented(rented);
        rentalTypeMapper.updateRentalType(rentalType);
        if (rented == needed) {
            //房屋租满,状态变为在租
            house.setIsRental("0");
            houseMapper.updateHouse(house);
        }
        return contractMapper.updateContract(contract);
    }

    /**
     * 批量删除合同信息
     *
     * @param contractIds 需要删除的合同信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteContractByContractIds(Long[] contractIds) {
        return contractMapper.deleteContractByContractIds(contractIds);
    }

    /**
     * 删除合同信息信息
     *
     * @param contractId 合同信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteContractByContractId(Long contractId) {
        return contractMapper.deleteContractByContractId(contractId);
    }

    /**
     * 用户申请租房
     * <p>
     * contract 合同对象
     */
    @Override
    @Transactional
    public void userContractApp(Contract contract) {
        contract.setUpdateTime(DateUtils.getNowDate());
        contractMapper.insertContract(contract);

        //邮件通知中介生成合同
        Medium medium = mediumMapper.selectMeidumByHouseId(contract.getHouseId());
        House house = houseMapper.selectHouseByHouseId(contract.getHouseId());
        //获取中介邮箱
        User user = userMapper.selectUserByUserId(medium.getUserId());

        String subject = "生成合同提醒";
        String text = "尊敬的中介用户" + user.getNickName() + ",您负责的房源" + house.getHouseName() + "已有人申请入住,请尽快生成租赁合同";
        mailSender.sendSimpleMail(user.getNickName(), house.getHouseName(), user.getEmail(), text, subject);
        log.info("已通知中介{}生成房屋{}的租赁合同", user.getNickName(), house.getHouseName());
    }
}
